package com.imooc.ecommerce.constant;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;
import java.util.stream.Stream;

/**
 * 商品类别
 * 电器 --> 手机、电脑
 * @author zhangshao
 * @since 2023/11/29 21:40
 */
@Getter
@AllArgsConstructor
public enum GoodsCategory {
    DIAN_QI("10001","电器"),
    JIA_JU("10002","家具"),
    FU_SHI("10003","服饰"),
    MU_YIN("10004","母婴"),
    SHI_PIN("10005","食品"),
    TU_SHU("10006","图书")
    ;
    /**
     * 商品类别编码
     */
    @EnumValue
    private final String code;

    /**
     * 商品类别描述信息
     */
    @JsonValue
    private final String description;

    /**
     * 根据code获取GoodsCategory
     */
    public static GoodsCategory of(String code){
        Objects.requireNonNull(code);
        return Stream.of(GoodsCategory.values())
                .filter(bean ->bean.code.equals(code))
                .findAny()
                .orElseThrow(
                ()-> new IllegalArgumentException(code +"not exist")
        );
    }
}
